<?php
/*
 * shop 控制器
 */
require_once APP_ROOT.'/include/action/commonAction.php';
class Shop_Action extends commonAction{

	function index(){
		$page_title = '店铺管理';
		extract($this -> options_cache);
		$site_title = $page_title . ' - ' . $site_name;

		$Shop_Model = new Shop_Model();
		$shopes = $Shop_Model -> getDatas(" ORDER BY sid DESC ");

		include View::getView('shop_index');
	}

	function edit(){
		$page_title = '添加店铺';
		extract($this -> options_cache);
		$site_title = $page_title . ' - ' . $site_name;
		
		$Ditches_Model = new Ditches_Model();
		$ditches = $Ditches_Model -> getDatas(" ORDER BY did ASC ");

		include View::getView('shop_edit');
	}

	function handle(){
		$token = isset($_REQUEST['token']) ? addslashes($_REQUEST['token']) : '';
        if ($token != LoginAuth::genToken()) {
            exit('{"code":108,"msg":"权限不足！"}');
        }

		$action = isset($_POST['action'])?trim($_POST['action']):'';
		$methodName = 'handle_'.$action;
		if(method_exists($this, $methodName)){
			$this -> $methodName();
		}
	}

	function handle_editShop(){

		//管理员权限限制

		$shopname = isset($_POST['shopname'])?addslashes(trim($_POST['shopname'])):'';
		$description = isset($_POST['description'])?addslashes(trim($_POST['description'])):'';
		$shopditches = $_POST['shopditches'];
		if(!$shopname){
			exit('{"result":101,"msg":"店铺名称必填！"}');
		}
		if(empty($shopditches)){
			exit('{"result":101,"msg":"至少给店铺选择一个渠道！"}');
		}
		$shopditches = serialize($_POST['shopditches']);
		$uids = serialize(array(1));

		$shopData = array(
			'shopname' => $shopname,
			'description' => $description,
			'shopditches' => $shopditches,
			'uids' => $uids,
			);

		$Shop_Model = new Shop_Model();
		$return_id = $Shop_Model -> addData($shopData);
		if($return_id){
			exit('{"result":200,"msg":"保存成功！"}');
		}
	}

}